package com.ship.edge.utils;

import cn.hutool.core.util.StrUtil;
import com.ship.edge.config.SystemConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Component
public class DirFileUtil {

    @Resource
    SystemConfig systemConfig;

    public String getRootPath() throws IOException {
        String osName = System.getProperty("os.name").toLowerCase();
        String directoryPath = null;
        if (osName.contains("win")) {
            directoryPath = systemConfig.getWinPath();
        }else{
            directoryPath = systemConfig.getLinuxPath();
        }
        //无则创建，有则不处理
        Files.createDirectories(Paths.get(directoryPath));
        return directoryPath;
    }

    public String getFileDatePath(Calendar cal, String fileName, String dateRax, String hourRax) throws IOException {
        StringBuffer sb = new StringBuffer();
        String directoryPath = getRootPath();
        sb.append(directoryPath);
        if(StrUtil.isNotEmpty(dateRax)){
            SimpleDateFormat sdf = new SimpleDateFormat(dateRax);
            String dateDir = sdf.format(cal.getTime());
            sb.append(File.separator + dateDir);
        }
        if(StrUtil.isNotEmpty(hourRax)){
            SimpleDateFormat sdf = new SimpleDateFormat(hourRax);
            String hourDir = sdf.format(cal.getTime());
            sb.append(File.separator + hourDir);
        }
        if(StrUtil.isNotEmpty(fileName)){
            sb.append(File.separator + fileName);
        }else{
            sb.append(File.separator);
        }
        return sb.toString();
    }

    public String minioPath(String moldName, String fileName){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        int date = cal.get(Calendar.DATE);
        String minIoFile = year + "/" + month + "/" + date;
        if(StrUtil.isNotEmpty(moldName)){
            minIoFile = minIoFile + "/" + moldName;
        }
        minIoFile = minIoFile + "/" + fileName;
        return minIoFile;
    }

}
